package com.ep.thirdPlatformProject.erp.helper;


import com.ep.thirdPlatformProject.erp.config.ErpConfig;
import com.ep.thirdPlatformProject.erp.entity.ErpCommonExecution;
import com.ep.thirdPlatformProject.erp.entity.ErpDayCostParam;
import com.ep.thirdPlatformProject.system.util.OkHttpUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;


@Component
@Slf4j
@RequiredArgsConstructor
public class ErpDayCostHelper {

    private final ErpCommonHelper commonHelper;

    private final ErpConfig erpConfig;

    /**
     * 发送post请求 结果为erp返回结果
     *
     * @param date        日期
     * @param dayAmount   天量
     * @param monthAmount 月数量
     * @param yearAmount  年金额
     * @return {@link String}
     */
    public String sendPostRequest(String date, String dayAmount, String monthAmount, String yearAmount) {

        String jsonParam = commonHelper.buildPayload(
                this.buildRequestParam(date, dayAmount, monthAmount, yearAmount),
                ErpDayCostParam.SERVER_NAME
        );


        String response = OkHttpUtils.builder()
                .url(erpConfig.getUrl())
                .postJsonRequest(jsonParam)
                .sync();
        ErpCommonExecution execution = commonHelper.parseResponseToRes(response);


        return execution.getDescription();
    }

    /**
     * 构建请求参数
     *
     * @param dateTime    日期时间
     * @param dayAmount   天量
     * @param monthAmount 月数量
     * @param yearAmount  年金额
     * @return {@link ErpDayCostParam}
     */
    private ErpDayCostParam buildRequestParam(String dateTime, String dayAmount, String monthAmount, String yearAmount) {
        // 获取本年开始时间
        return new ErpDayCostParam()
                .setXcaauc002(dateTime)
                .setXcaauc003(dayAmount)
                .setXcaauc004(monthAmount)
                .setXcaauc005(yearAmount);
    }

}
